<?php
namespace app\home\controller;

error_reporting(E_ALL ^ E_NOTICE);
use app\admin\model\Config;
use app\home\model\Member;
use app\home\model\Recharge as UserRecharge;
use app\XDeode;
use think\Db;
use think\Controller;
class Alipay extends Base
{
    public function pay(){
        require_once APP_PATH.'../'.'extend/alipay/wappay/service/AlipayTradeService.php';
        require_once APP_PATH.'../'.'extend/alipay/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php';
        require_once APP_PATH.'../'.'extend/alipay/config.php';
    //$orderNo = md5(uniqid());//$_var_482['order_no'];//
//     if(!$orderNo){
// 		return json(['code' => 0, 'msg' => '订单号不能为空']);
//     }
        //商户订单号，商户网站订单系统中唯一订单号，必填
    $out_trade_no = $this->create_order_no();//$_var_482['WIDout_trade_no'];
    $member = $this->checkLogin();
    $userId = $member['uid'];
    if(!$userId){
		return json(['code' => 0, 'msg' => '用户ID不能为空']);
    }
    //订单名称，必填
    $subject = "充值";
    if(!$subject){
		return json(['code' => 0, 'msg' => '订单名称不能为空']);
    }
    //付款金额，必填
    $total_amount =$_REQUEST['amount'];
    //$total_amount = 0.01;
    if($total_amount<=0){
		return json(['code' => 0, 'msg' => '订单金额不合法']);
    }
//     $productId = $_var_482['product_id'];
//     if(!$productId){
// 		return json(['code' => 0, 'msg' => '产品id不能为空']);
//     }
//     $name = $_var_482['name'];
//     $name = "vippppp";
//     if(!$name){
// 		return json(['code' => 0, 'msg' => 'vip名称不能为空']);
//     }
    //商品描述，可空
    $body = "213678";
    
    $order_no=$out_trade_no;
    $status3 = UserRecharge::addInfo([
        'uid' => $member['uid'],
        'username' => $member['username'],
        'credit2' => $total_amount,
        'realname' => '',
        'account' => $order_no,
        'pay_method'=>1,//支付宝
        'pay_time' => '',
        'thumbs' => '',
        'create_time' => TIMESTAMP,
        'jilu'=>date('Ymd')
    ]);
    // $data['order_no'] = $orderNo;
    // $data['money'] = $total_amount;
    // $data['user_id'] = $userId;
    // $data['title'] = $subject;
    // $data['pay_type'] = 1;
    // $data['status'] = 1;
    // $data['add_time'] = time();
    // $data['product_id'] = 0;//$productId;
    // $data['name'] = $name;
    // $exist = db('order')->where('order_no="'.$orderNo.'"')->find();
    // if($exist){
    //     return json(['code' => 0, 'msg' => '订单异常']);
    // }
    // db('order')->insert($data);
    
    //超时时间
    $timeout_express="1m";

    $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
    $payRequestBuilder->setBody($body);
    $payRequestBuilder->setSubject($subject);
    $payRequestBuilder->setOutTradeNo($out_trade_no);
    $payRequestBuilder->setTotalAmount($total_amount);
    $payRequestBuilder->setTimeExpress($timeout_express);

    $payResponse = new \AlipayTradeService($config);
    $result=$payResponse->wapPay($payRequestBuilder,$config['return_url'],$config['notify_url']);
    
    return;
        
    }
    
     //生成订单号
    public  function create_order_no(){
            //生成24位唯一订单号码，格式：YYYY-MMDD-HHII-SS-NNNN,NNNN-CC，其中：YYYY=年份，MM=月份，DD=日期，HH=24格式小时，II=分，SS=秒，NNNNNNNN=随机数，CC=检查码
             @date_default_timezone_set("PRC");
             //订购日期
                $order_date = date('Y-m-d');
                //订单号码主体（YYYYMMDDHHIISSNNNNNNNN）
                $order_id_main = date('YmdHis') . rand(10000000,99999999);
                //订单号码主体长度
                $order_id_len = strlen($order_id_main);
                $order_id_sum = 0;
                for($i=0; $i<$order_id_len; $i++){
                                $order_id_sum += (int)(substr($order_id_main,$i,1));
                }
                //唯一订单号码（YYYYMMDDHHIISSNNNNNNNNCC）
                $order_id = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT);
                return $order_id;
    }
    
}